# -*- coding: utf-8 -*-
"""
 @Time    : 2021/1/25 下午6:23
 @Author  : tgy
 
"""

class Node:
    __slots__ = ['childen','val']

    def __init__(self, val):
        self.val = val
        self.childen = []

    def add_child(self,node):
        ...
        if not isinstance(node,Node):
            raise TypeError('node must Node type')
        self.childen.append(node)

    def __iter__(self):
        return iter(self.childen)

    def depth_first(self):

        yield self
        for child in self:
            ...
            yield from child.depth_first()


if __name__ == '__main__':
    ...
    node = Node(1)
    node.add_child(Node(2))
    node_03 = Node(3)
    node.add_child(node_03)
    node.add_child(Node(4))
    node_03.add_child(Node(5))

    for no in node.depth_first():
        print(no.val)

